package com.gxa.jjty.admin.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.mapper.MsgTypeMapper;
import com.gxa.jjty.admin.service.MsgTypeService;
import com.gxa.jjty.admin.service.SystemMsgService;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.MsgType;
import com.gxa.jjty.core.pojo.SystemMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author asus
 */

@RestController
@RequestMapping("/message")
@Api(tags = "系统消息")
@Log4j2
public class MessageController {

    @Autowired
    SystemMsgService systemMsgService;

    @Autowired
    MsgTypeService msgTypeService;

    @PostMapping("/getMessageManage")
    @ApiOperation("查询消息管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "每页的记录数"),
            @ApiImplicitParam(name = "sReleaseTime",value = "开始发布时间"),
            @ApiImplicitParam(name = "eReleaseTime",value = "结束发布时间"),
            @ApiImplicitParam(name = "publisher",value = "发布人"),
            @ApiImplicitParam(name = "title",value = "标题")
    })
    public Map selectMessage( Integer page, Integer size, Date sReleaseTime, Date eReleaseTime, String publisher, String title)
    {

        PageInfo<SystemMsg> info = systemMsgService.selectSM(page, size, sReleaseTime, eReleaseTime, publisher, title);
        long total = info.getTotal();
        List<SystemMsg> list = info.getList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",total);
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/putMessage")
    @ApiOperation("发布消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "类型"),
            @ApiImplicitParam(name = "title",value = "标题"),
            @ApiImplicitParam(name = "content",value = "发布内容")

    })
    public Map insertRecital(@RequestBody SystemMsg systemMessage) throws BusinessException {

        systemMessage.setReleaseTime(new Date());
        systemMsgService.insertSystemMsg(systemMessage);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("发布成功");
        return jsonResult.getDataMap();
    }


    @PostMapping("/getByIdMessage")
    @ApiOperation("查看消息明细")
    public Map insertRecital(@RequestParam("id") Long id){
        SystemMsg systemMsg = systemMsgService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(systemMsg);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getAll")
    @ApiOperation("查询所有消息类型的名称")
    public Map selectAllType(){
        List<MsgType> msgTypes = msgTypeService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",msgTypes.size());
        jsonResult.setData(msgTypes);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getMsgType")
    @ApiOperation("根据条件查询消息类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "每页记录数"),
            @ApiImplicitParam(name = "typeStatus",value = "类型状态"),
            @ApiImplicitParam(name = "typeName",value = "类型名称")

    })
    public  Map selectMsgType(Integer page,Integer size,Integer typeStatus,String typeName) throws IllegalAccessException {

        MsgType msgType = new MsgType();
        msgType.setMessageStatus(typeStatus);
        msgType.setTypeName(typeName);
        PageInfo<MsgType> msgTypePageInfo = msgTypeService.selectByPage(page, size, msgType);
        long total = msgTypePageInfo.getTotal();
        List<MsgType> msgTypes = msgTypePageInfo.getList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",total);
        jsonResult.setData(msgTypes);
        return jsonResult.getDataMap();
    }


    @PostMapping("/getMessage")
    @ApiOperation("消息中心")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "每页记录数"),
            @ApiImplicitParam(name = "title",value = "标题"),
            @ApiImplicitParam(name = "msgStatus",value = "状态")

    })
    public Map selectType(   Integer page, Integer size, String  title, Integer msgStatus
                            )

    {

        PageInfo<SystemMsg> systemMsgPageInfo = systemMsgService.selectMessage(page, size, title, msgStatus);
        List<SystemMsg> msgList = systemMsgPageInfo.getList();
        long total = systemMsgPageInfo.getTotal();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",total);
        jsonResult.setData(msgList);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getByStatus")
    @ApiOperation("全部已读")
    public Map selectByStatus( Integer page,
                                Integer size,
                                 Integer msgStatus
                                ) throws IllegalAccessException {
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setMessageStatus(msgStatus);
        PageInfo<SystemMsg> systemMsgPageInfo = systemMsgService.selectByPage(page, size, systemMsg);
        List<SystemMsg> msgList = systemMsgPageInfo.getList();
        long total = systemMsgPageInfo.getTotal();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",total);
        jsonResult.put("msgList",msgList);
        return jsonResult.getDataMap();
    }


    @PostMapping("/setRecital")
    @ApiOperation("编辑消息类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeName",value = "类型名称"),
            @ApiImplicitParam(name = "messageStatus",value = "状态"),
            @ApiImplicitParam(name = "sort",value = "排序"),
            @ApiImplicitParam(name = "id",value = "id号")
    })
    public  Map updateType(@RequestBody MsgType msgType) throws IllegalAccessException {
        msgTypeService.updateById(msgType);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("更新成功");
        return jsonResult.getDataMap();

    }

    @PostMapping("/putType")
    @ApiOperation("添加消息类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeName",value = "类型名称"),
            @ApiImplicitParam(name = "messageStatus",value = "状态"),
            @ApiImplicitParam(name = "sort",value = "排序"),
    })
    public Map insertType(@RequestBody MsgType msgType){

        msgType.setCreationTime(new Date());
        msgTypeService.insert(msgType);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("添加成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteType")
    @ApiOperation("删除消息类型")
    public Map deleteType(@RequestParam("id") Long id){
        msgTypeService.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("删除成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getByIdType")
    @ApiOperation("查看消息类型明细")
    public Map selectType(@RequestParam("id") Long id){

        SystemMsg systemMsg = systemMsgService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(systemMsg);
        return jsonResult.getDataMap();
    }



}
